<?php
namespace Resmodel;

use Model\LanguageAwareInterface;
use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Predicate\Like;
use Zend\Db\Sql\Predicate\Operator;
use Zend\Db\Sql\Select;
use Zend\InputFilter\InputFilter;

class DishLangTable extends ResAbstractTable implements LanguageAwareInterface
{
    private $_lang;
    /**
     * @param $id
     * @return DishLang
     */
    public function getDishLang($id){
       return $this->getById($id);
    }

    /**
     * @param $name
     * @return DishLang
     */
    public function getDishLangByName($name)
    {
       return $this->getByMultiColumn(
           array('name'=>$name , 'lang'=>$this->getLang()))->current();
    }

    /**
     * @param $name
     * @return DishLang
     */
    public function getDishLangByAlias($alias)
    {
        return $this->getByMultiColumn(
            array('alias'=>$alias , 'lang'=>$this->getLang()))->current();
    }

    /**
     * @return InputFilter
     */
    public function getInputFilter()
    {
        $filter = new InputFilter();


        return $filter;
    }

    public function searchAllDishLang($isPaging, $offset, $rowPerPage, $orderArr = array('name'), $searchOption){

        $select = $this->getDbSqlSelect();

        if($searchOption['name']){
            $select->where(array('name like "%'.$searchOption['name'].'%"'));
        }

        if($isPaging){
            $select->offset($offset)->limit($rowPerPage);
        }

        if($orderArr){
            $select->order($orderArr);
        }

        //generate sql string to debug.log file
        \Logger::debug($select->getSqlString($this->tableGateway->adapter->getPlatform()));

        $rows = $this->tableGateway->selectWith($select);
        $result['rows'] = $rows;

        $total = $this->getCount($select);

        $result['total'] = $total;

        return $result;


    }

    public function getLang()
    {
        return $this->_lang;
    }

    public function setLang($lang)
    {
        $this->_lang = $lang;
    }

    /**
     * @param $id
     * @return DishLang|null
     */
    public function getByDishId($id)
    {
        return $this->getTableGateway()->select(
            array('lang'=>$this->_lang, 'dish_id'=>$id))->current();
    }

}